package cn.edu.neusoft.wuguangsheng.market.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.market.R;
import cn.edu.neusoft.wuguangsheng.market.adapter.AllCommentAdapter;
import cn.edu.neusoft.wuguangsheng.market.bean.AllCommentBean;
import cn.edu.neusoft.wuguangsheng.market.bean.FoodByIdBean;
import cn.edu.neusoft.wuguangsheng.market.bean.RegisterBean;
import cn.edu.neusoft.wuguangsheng.market.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.market.listener.TListener;
import cn.edu.neusoft.wuguangsheng.market.model.AddCartModel;
import cn.edu.neusoft.wuguangsheng.market.model.AllCommentModel;
import cn.edu.neusoft.wuguangsheng.market.model.FoodByIdModel;

public class FoodByIdActivity extends BaseActivity {
    ImageView imgret;
    TextView tv_food_name;
    ImageView img_food_pic;
    TextView food_js;
    TextView food_jj;
    Button foodBuy;
    String foodpic;
    String fid;
    public  int i=1;
    String foodnames, foodintro;
    String foodprice;
    TextView tvnum;
    String mrid;
    ImageView imgon,imgup;
    double price;
    RecyclerView recycle_food_comment;
    Button btn_cart;


    private List<AllCommentBean> items;

    TListener<FoodByIdBean> tListener = new TListener<FoodByIdBean>() {
        @Override
        public void onResponse(FoodByIdBean foodByIdBean) {
            tv_food_name.setText(foodnames);
            price=foodByIdBean.getPrice();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override

    void initView() {
        setLayout_file(R.layout.activity_food_by_id);
        imgret=(ImageView)findViewById(R.id.img_foodret);
        tv_food_name = (TextView) findViewById(R.id.tv_food_id1);
        img_food_pic = (ImageView) findViewById(R.id.img_food_id);
        food_js = (TextView) findViewById(R.id.et_food_id2);
        food_jj = (TextView) findViewById(R.id.et_food_id3);
        foodBuy = (Button) findViewById(R.id.btn_food_id);
        tvnum=(TextView)findViewById(R.id.tvnum);
        btn_cart=(Button) findViewById(R.id.btn_cart);

        recycle_food_comment=(RecyclerView)findViewById(R.id.recycle_food_comment);

        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(FoodByIdActivity.this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycle_food_comment.setLayoutManager(linearLayoutManager);

        foodpic = getIntent().getStringExtra("pic");
        fid = getIntent().getStringExtra("fid");
        foodintro = getIntent().getStringExtra("intro");
        foodnames = getIntent().getStringExtra("fname");
        foodprice = getIntent().getStringExtra("price");
        mrid = getIntent().getStringExtra("mrid");
        imgon=(ImageView) findViewById(R.id.imgon);
        imgup=(ImageView) findViewById(R.id.imgup);

        FoodByIdModel foodByIdModel = new FoodByIdModel();
        foodByIdModel.getFoodByIdMessage(fid,tListener);

        Picasso.with(FoodByIdActivity.this).load(foodpic).into(img_food_pic);

        food_jj.setText("￥"+foodprice.toString());
        food_js.setText("介绍:"+foodintro);

    }


    @Override
    void initEvent() {
        foodBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uid=getid();
                String f=tvnum.getText().toString();
                String num=String.valueOf(i);
                double sum=i*price;
                Intent  intentf=new Intent(FoodByIdActivity.this,BuyActivity.class);
                intentf.putExtra("sum",sum);
                intentf.putExtra("fid",fid);
                intentf.putExtra("num",num);
                intentf.putExtra("mrid",mrid);
                startActivity(intentf);
            }
        });
        imgret.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    imgon.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        odd();
    }
     });
        imgup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add();
            }
        });
        btn_cart.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        String uid=getid();
        String f=tvnum.getText().toString();
        String num=String.valueOf(i);
            double sum=i*price;
        AddCartModel addCartModel=new AddCartModel();
        addCartModel.addCart(uid,fid,num,sum,mrid,addListener);

    }
});



    }

    @Override
    void initDate() {
          fid=getIntent().getStringExtra("fid");
        AllCommentModel allCommentModel=new AllCommentModel();
        allCommentModel.showAllComment(fid,allCommentBeanListListener);

    }


        TListener<RegisterBean> addListener=new TListener<RegisterBean>() {
            @Override
            public void onResponse(RegisterBean registerBean) {
                String success=registerBean.getSuccess();
                if(success.equals("1")){
                    Toast.makeText(FoodByIdActivity.this,"加入购物车成功",Toast.LENGTH_SHORT).show();
                }
                else if(success.equals("2")) {
                    Toast.makeText(FoodByIdActivity.this, "购物车中已有该菜品", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(FoodByIdActivity.this, "加入购物车失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        ListListener<AllCommentBean> allCommentBeanListListener=new ListListener<AllCommentBean>() {
            @Override
            public void onResponse(List<AllCommentBean> list) {
                items=list;
                //CommentsAdapter commentsAdapter=new CommentsAdapter(UserAllCommentActivity.this,items,R.layout.commentscard);
                AllCommentAdapter allCommentAdapter=new AllCommentAdapter(FoodByIdActivity.this,items,R.layout.yhcommentcard);
                recycle_food_comment.setAdapter(allCommentAdapter);
                recycle_food_comment.setItemAnimator(new DefaultItemAnimator());
            }

            @Override
            public void onFail(String msg) {

            }
        };


        public void add(){
            i++;
            String s = String.valueOf(i);
            tvnum.setText(s);
        }

        public void odd(){

            if(i>1){
                i--;
                String s = String.valueOf(i);
                tvnum.setText(s);
            }
            else if(i==1){
                i=1;
                String m = String.valueOf(i);
                tvnum.setText(m);
            }
        }


    }
